<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/coupon' }"
        >优惠券管理</el-breadcrumb-item
      >
      <el-breadcrumb-item>修改优惠券</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <div class="form_title">
        <h5>修改优惠券</h5>
      </div>
      <!-- 修改优惠券表单 -->
      <el-form ref="form" :model="form" label-width="120px" :rules="formRules">
        <el-form-item label="分类：">
          <el-col :span="12">
            <el-select
              v-model="form.range"
              filterable
              placeholder="请选择"
              value-key="id"
              style="width: 100%"
              @change="select"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="优惠券额度：">
          <el-col class="line" :span="1">满</el-col>
          <el-col :span="5">
            <el-input
              v-model="full"
              type="number"
              :disabled="sureDisabled"
            ></el-input>
          </el-col>
          <el-col class="line" :span="1">减</el-col>
          <el-col :span="5">
            <el-input
              v-model="reduce"
              type="number"
              :disabled="sureDisabled"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="兑换积分：">
          <el-col :span="11">
            <el-input v-model="form.integral" type="number"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="有效期：" prop="day">
          <el-col :span="11">
            <el-input v-model="form.day" type="number">
              <template slot="append">天</template>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="介绍：" prop="explain">
          <el-col :span="11">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.explain"
            >
            </el-input>
          </el-col>
        </el-form-item>

        <el-form-item class="formbutton">
          <el-button class="formsubmit" type="primary" @click="onSubmit"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labelPosition: "right",
      typeList: [
        { id: 1, name: "奶茶满减劵" },
        { id: 2, name: "商品满减劵" },
        { id: 3, name: "商品兑换劵" },
        // { id: 4, name: "猫劵" },
      ],
      form: {
        day: "",
        full: "",
        reduce: "",
        integral: "",
        range: 1,
        explain: "",
      },
      full:"",
      reduce:"",
      sureDisabled: false,
      formRules: {
        // 验证表单是否合法
        // integral: [
        //   { required: true, message: "请输入兑换积分", trigger: "blur" },
        // ],
        day: [{ required: true, message: "请输入有效期", trigger: "blur" }],
        explain: [
          { required: true, message: "请输入优惠券简述", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async getCouponById(id) {
      const { data: res } = await this.$http.get(
        "coupon/getCouponById?id=" + id
      );

      if (res.code != 200) {
        return this.$message.error(res.massage);
      }
      this.form.id = res.data.coupon.id;
      this.form.range = res.data.coupon.range;
      this.full = res.data.coupon.full/100;
      this.reduce = res.data.coupon.reduce/100;
      this.form.integral = res.data.coupon.integral;
      this.form.explain = res.data.coupon.explain;
      this.form.day = res.data.coupon.day;
      if (this.form.range == 3 || this.form.range == 4) {
        this.sureDisabled = true;
      }
    },
    select() {

      if (this.form.range !== 1 && this.form.range !== 2) {
        this.sureDisabled = true;
      } else {
        this.sureDisabled = false;
      }
      // if(this.form.range !== 3 && this.form.range !== 4){
      //   this.sureDisabled = false;
      // }
    },
    //提交调用相应的接口
    onSubmit() {
      this.$refs.form.validate(async (valid) => {


        if (valid) {
          if (this.full === "") {
            this.full = "0";
          }
          if (this.reduce === "") {
            this.reduce = "0";
          }
          if (this.form.integral === "") {
            this.form.integral = "0";
          }

          this.form.full = this.full*100;
          this.form.reduce = this.reduce*100;
          // 调用提交接口
          const { data: res } = await this.$http.post(
            "coupon/updateCoupon",
            this.form
          );
          if (res.code !== 200) {
            this.$message.error(res.massage);
            return;
          }
          this.$message.success("修改优惠券成功！");
          this.$router.replace("/coupon");
        } else {

          return;
        }
      });
    },
  },
  created() {
    this.getCouponById(this.$route.params.id);
  },
};
</script>

<style lang="less" scoped>
.form_title {
  height: 40px;
  text-align: left;
  line-height: 0px;
  letter-spacing: 1px;
  border-left: rgba(64, 158, 255, 0.781) solid 3px;
  box-shadow: 0 3px 8px #ddd;
  padding-left: 10px;
  margin-top: 0px;
  overflow: hidden;
}

.el-form {
  width: 800px;
  margin: 50px auto;
  text-align: center;
  padding-left: 350px;
}
.formbutton,
.state {
  position: relative;
}
.formbutton .formsubmit,
.state .stateRadio {
  position: absolute;
  left: 80px;
}
</style>
